package com.echo.activity;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.echo.SportsApplication;
import com.echo.adapter.FriendAdapter;
import com.echo.http.HttpCallBack;
import com.echo.http.HttpManager;
import com.echo.http.HttpUrl;
import com.echo.model.User;
import com.echo.util.DrawableUtil;
import com.echo.util.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

public class FriendsActivity extends AppCompatActivity{

    private RecyclerView mFriendsRecycler;
    /*private QuickSideBarView quickSideBarView;
    private QuickSideBarTipsView quickSideBarTipsView;*/
    private FriendAdapter mAdapter;

    private Toolbar mToolbar;
    private TextView mToolTitle;

    private PtrFrameLayout mPtrFrame;
    private TextView mRequestNumber;

    private List<User> friends=new ArrayList<User>(),requests=new ArrayList<User>();
    //private String getFriendsListURL= HttpUrl.URL_GET_FRIENDS+"?id="+ SportsApplication.getInstance().getUserId();
    private String newFriendURL=HttpUrl.URL_NEW_FRIEND+"?id="+SportsApplication.getInstance().getUserId()+"&username=";
    private String getFriendRequestNumber=HttpUrl.URL_GET_FRIEND_REQUEST_NUMBE+"?id=";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friends);

        initActionBar();
        /*quickSideBarTipsView= (QuickSideBarTipsView) findViewById(R.id.quickSideBarTipsView);
        quickSideBarView= (QuickSideBarView) findViewById(R.id.quickSideBarView);
        quickSideBarView.setOnQuickSideBarTouchListener(this);*/
        /*mAdapter=new FriendsWithHeaderAdapter();
        mFriendsRecycler.setAdapter(mAdapter);

        final StickyRecyclerHeadersDecoration headersDecor = new StickyRecyclerHeadersDecoration(mAdapter);
        mFriendsRecycler.addItemDecoration(headersDecor);
        mFriendsRecycler.addItemDecoration(new DividerDecoration(this));*/


        initView();
        initData();

        mAdapter=new FriendAdapter();
        mFriendsRecycler.setAdapter(mAdapter);
    }

    private void initView() {
        mPtrFrame= (PtrFrameLayout) findViewById(R.id.friends_refresh);
        mFriendsRecycler= (RecyclerView) findViewById(R.id.friends_recycler);
        mFriendsRecycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false));
        mRequestNumber= (TextView) findViewById(R.id.friend_request_number);
        mPtrFrame.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return false;
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                initData();
                frame.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPtrFrame.refreshComplete();
                    }
                },1800);
            }
        });
    }

    private void initData() {
        String getFriendsListURL= HttpUrl.URL_GET_FRIENDS+"?id="+ SportsApplication.getInstance().getUserId();
        HttpManager.getInstance().get_friend(getFriendsListURL, new HttpCallBack() {
            @Override
            public void onMsg(Object response) throws JSONException {
                JSONArray jsonArray=(JSONArray)response;
                Gson gson=new Gson();
                friends = gson.fromJson(jsonArray.toString(),
                        new TypeToken<List<User>>() {
                        }.getType());
                mAdapter=new FriendAdapter(FriendsActivity.this,friends);
                mFriendsRecycler.setAdapter(mAdapter);
            }

            @Override
            public void onError() {

            }
        });
        HttpManager.getInstance().get_friend_request_number(getFriendRequestNumber, new HttpCallBack() {
            @Override
            public void onMsg(Object response) throws JSONException {
                JSONObject jsonObect=(JSONObject)response;
                if(jsonObect.getString("message").equals("success")){
                    int number=jsonObect.getInt("number");
                    if(number==0)
                        mRequestNumber.setText("");
                    else
                        mRequestNumber.setText(String.valueOf(number));
                }else{
                    ToastUtils.show(FriendsActivity.this,jsonObect.getString("message"), Toast.LENGTH_SHORT);
                }
            }

            @Override
            public void onError() {

            }
        });
    }

    private void initActionBar() {
        mToolbar= (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(DrawableUtil.tintDrawable(getResources()
                .getDrawable(R.mipmap.ic_arrow_back_black_36dp), Color.parseColor("#8a041835")));
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                return false;
            }
        });
        mToolTitle= (TextView) findViewById(R.id.toolbar_title);
        mToolTitle.setText("好友");
    }

    /*@Override
    public void onLetterChanged(String letter, int position, int itemHeight) {

    }

    @Override
    public void onLetterTouching(boolean touching) {

    }

    public class FriendsWithHeaderAdapter extends FriendsAdapter<RecyclerView.ViewHolder>
            implements StickyRecyclerHeadersAdapter<RecyclerView.ViewHolder>{

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_friend, parent, false);
            return new RecyclerView.ViewHolder(view) {
            };
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        }

        @Override
        public long getHeaderId(int position) {
            return 0;
        }

        @Override
        public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.header_friend, parent, false);
            return new RecyclerView.ViewHolder(view) {
            };
        }

        @Override
        public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {

        }
    }*/
}
